var express = require('express');
var router = express.Router();
var multiparty = require('multiparty');
let { UserM, ljyshopping,Category,kang,weter,trade,after} = require('../../modules/model')
//分类添加
// const data=[
//   {title:"日常清洁",
//   img:"https://ts3.cn.mm.bing.net/th?id=OIP-C.1puqI7k2Vi3HkDyR46pa7QHaE7&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   number:1,
// },
// {
//   title:'临床护理',
//   img:'https://ts3.cn.mm.bing.net/th?id=OIP-C.w0EOLTXc66FImb7YA5rRQQHaFu&w=284&h=219&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2',
//   number:2
// },{
//   title:"康复护理",
//   img:"https://ts1.cn.mm.bing.net/th?id=OIP-C.5ouW-X1PemPtPKnZ1afU_wHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   number:3
// },
// {
//   title:"心里关怀",
//   img:"https://ts1.cn.mm.bing.net/th?id=OIP-C.MnfJOfOAvgeO-yDZYlQJbgHaE8&w=176&h=185&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   number:4
// },
// {
//   title:"上门做饭",
//   img:'https://ts2.cn.mm.bing.net/th?id=OIP-C.32ognDNKfb3rTroMwtGX9AHaFS&w=295&h=211&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2',
//   number:5
// },
// {
//   title:"健康管理",
//   img:"https://ts4.cn.mm.bing.net/th?id=OIP-C.RWTFgFH2c-xQ7hPD1ELK5wHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   number:6
// },
// {
//   title:"生活照料",
//   img:"https://ts2.cn.mm.bing.net/th?id=OIP-C.I5kc9Jotpok0bIzDL04dSwHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   number:7
// },
// {
//   title:"陪同就医",
//   img:"https://ts2.cn.mm.bing.net/th?id=OIP-C.5bMTviHcM3Tfr8h_7erxswHaED&w=338&h=184&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   number:7,
// }]
// Category.create(data)
router.get('/ljyshow',async(req,res)=>{
    let data = await ljyshopping.find().populate('cate')
    res.send({
      code:200,
      data:data,
    })
})
router.post('/select', async (req, res) => {
  try {
    // 获取请求体中的查询条件
    const { cate, time, title, da, xiao } = req.body;
    console.log('查询的数据:', cate, time, title, da, xiao);
    // 动态构建查询条件对象
    const query = {};
    if (cate && cate.trim() !== '') query.cate = cate; // 如果 cate 不为空
    if (time && time.trim() !== '') query.time = time; // 如果 time 不为空
    if (title && title.trim() !== '') query.title = { $regex: title}; // 模糊匹配 title
    if (da && xiao) {
      query.price = { $gte: parseFloat(xiao), $lte: parseFloat(da) }; // 价格范围查询
    } else if (da) {
      query.price = { $gte: parseFloat(da) }; // 最小价格查询
    } else if (xiao) {
      query.price = { $lte: parseFloat(xiao) }; // 最大价格查询
    }
    // $gt: 大于, $gte: 大于等于, $lt: 小于, $lte: 小于等于
    console.log('最终查询条件:', query);

    // 执行查询
    const data = await ljyshopping.find(query);
    console.log(data);
    // 返回查询结果
    res.send({
      code: 200,
      data: data,
    })
  } catch (error) {
    console.error('查询出错:', error);
    res.status(500).json({ success: false, message: '服务器错误' });
  }
});
//图片上传
router.post('/upload',async (req, res) => {
  let form = new multiparty.Form
  form.uploadDir = './upload'
  form.parse(req, (err, formdate, data) => {
    console.log('data',data);
     res.send({
      code:200,
       path:'http://127.0.0.1:3000/'+data.avatar[0].path
     })
  })
  // console.log('上传图片',form);
})
//添加数据
router.post('/add',async(req,res)=>{
  let data= req.body
  console.log('添加数据',data);
  await ljyshopping.create(data)
  // let list= await ljyshopping.find()
  // res.send({
  //   code:200,
  //   data:list
  // })
})
router.post('/update',async(req,res)=>{
  let data= req.body
  console.log('修改数据',data);
  await ljyshopping.updateOne({_id:data.id},data)
})
router.post('/jia',async(req,res)=>{
  let id= req.body.id
  let data= await ljyshopping.find({_id:id})
  if(data[0].state==false){
    data[0].state=true
    console.log('data',data[0].state);
  }else{
    data[0].state=false
    console.log('data11111',data[0].state);
  }
  console.log('修改数据',id);
  console.log('data',data[0]);
  await ljyshopping.updateOne({_id:id},data[0])
})
router.post('/del',async(req,res)=>{
  let id= req.body.id
  console.log('删除数据',id);
  await ljyshopping.deleteOne({_id:id})
})
router.get('/cate',async(req,res)=>{
  let data = await Category.aggregate([
    {
      $lookup: {
        from: "ljyshopping", // 要关联的集合名称（子表）
        localField: "_id",   // 当前集合（父表）中的字段名
        foreignField: "cate", // 子表中与父表关联的字段名
        as: "list"           // 查询结果存储的字段名（数组形式）
      }
    }
  ]);
  console.log('list',data);
  res.send(data)
})
//康复管理数据
// const data=[{
//    title:'日常清洁',
//    img:"https://ts3.cn.mm.bing.net/th?id=OIP-C.Ly7hfCmf6dPXmpYJYcenAAHaE7&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//    price:'100',
//    tou:'https://ts1.cn.mm.bing.net/th?id=OIP-C.jzk9_90bqk7e32qyvxYvggHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2',
//    phone:"15128897670",
//    state:1,
//    zhi:"支付宝",
//    name:"张三",
//    time:"2023-10-10",
// },
// {
//   title:"康复训练",
//   img:"https://ts3.cn.mm.bing.net/th?id=OIP-C.Jmwojx5sdQa6HFkNkABhSQHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   price:"300",
//   tou:"https://ts2.cn.mm.bing.net/th?id=OIP-C.7yPYhyuDSNF2ILIt8otzMgHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   phone:"15130427670",
//   state:2,
//   zhi:'微信',
//   name:'李四',
//   time:"2023-10-10",
// },
// {
//   title:"打扫卫生",
//   img:"https://ts4.cn.mm.bing.net/th?id=OIP-C.M7DaOMYxzAXBrZJ9red2qQHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   price:'200',
//   tou:"https://ts3.cn.mm.bing.net/th?id=OIP-C.rsTOhXgAfsEGT4VzyhgYeQHaF7&w=279&h=223&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   phone:"12345678911",
//   state:3,
//   zhi:"银行卡",
//   name:"王五",
//   time:"2025-01-11",
// },
// {
//   title:"陪同就医",
//   img:"https://ts3.cn.mm.bing.net/th?id=OIP-C.zQ7i4jDKCd5qCAVf9UaemQHaE7&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   price:"400",
//   tou:"https://ts1.cn.mm.bing.net/th?id=OIP-C.uA9k1mTppAaq59zi6cLvxQHaHa&w=250&h=250&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   phone:"1503258628",
//   state:4,
//   zhi:'微信',
//   name:"丽丽",
//   time:"2023-10-10",

// },
// {
//    title:"陪老人散步",
//    img:"https://ts4.cn.mm.bing.net/th?id=OIP-C.c_JAwQ1jTQ8h_aFnC-dWngHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//    price:'200',
//    tou:"https://ts1.cn.mm.bing.net/th?id=OIP-C.orV8l0lQbdAzSabQeilJ6gHaE7&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//    phone:"852963741",
//    state:5,
//    zhi:'支付宝',
//    name:'小红',
//    time:"2023-10-10",
//   },
// {
//   title:'打扫厨房卫生',
//   img:'https://ts1.cn.mm.bing.net/th?id=OIP-C.APjRZdlH0UtHBWXPtxSXZgHaE7&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2',
//   price:'300',
//   tou:"https://ts1.cn.mm.bing.net/th?id=OIP-C.APjRZdlH0UtHBWXPtxSXZgHaE7&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   phone:'852963741',
//   state:6,
//   zhi:"支付宝",
//   name:'小亮',
//   time:"2023-10-10",
// }]
// trade.create(data)
router.get('/kang',async(req,res)=>{
  let data = await kang.find().populate('cate')
  console.log('list',data);
  res.send(data)
})
router.get('/cate1',async(req,res)=>{
  let data = await weter.aggregate([
    {
      $lookup: {
        from: "kang", // 要关联的集合名称（子表）
        localField: "_id",   // 当前集合（父表）中的字段名
        foreignField: "cate", // 子表中与父表关联的字段名
        as: "list"           // 查询结果存储的字段名（数组形式）
      }
    }
  ]);
  console.log('list',data);
  res.send(data)
})
router.post('/add1',async(req,res)=>{
  let data= req.body
  console.log('添加数据',data);
  await kang.create(data)
  // let list= await ljyshopping.find()
  // res.send({
  //   code:200,
  //   data:list
  // })
})
router.get('/jiao',async(req,res)=>{
  let data=await trade.find()
  res.send(data)
})
//售后管理数据
// const data=[{
//   title:'日常清洁',
//   img:"https://ts4.cn.mm.bing.net/th?id=OIP-C.1iQx7zJ8WRoxZvRbLF2XDQHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   price:100,
//   price1:100,
//   state:'处理中',
//   time:'2023-10-10',

// },
// {
//   title:'打扫厨房',
//   img:"https://ts2.cn.mm.bing.net/th?id=OIP-C.AoOFwLHWM2E2JbcZQPX-WgHaE7&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   price:200,
//   price1:200,
//   state:'售后已完成',
//   time:'2023-10-10',
// },
// {
//   title:"打扫客厅",
//   img:"https://ts3.cn.mm.bing.net/th?id=OIP-C.8EEwkWgGUCys2WAELgpzSwHaEu&w=313&h=199&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   price:300,
//   price1:300,
//   state:'售后关闭',
// },
// {
//   title:"陪同就医",
//   img:"https://ts3.cn.mm.bing.net/th?id=OIP-C.zQ7i4jDKCd5qCAVf9UaemQHaE7&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   price:400,
//   price1:400,
//   state:'处理中',
//   time:'2023-10-10',
// },
// {
//   title:'康复理疗',
//   img:"https://ts3.cn.mm.bing.net/th?id=OIP-C.AplcwjU2DASpNfiOHzksCgHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2",
//   price:500,
//   price1:500,
//   state:'售后完成',
// },
// {
//   title:"做饭",
//   img:'https://ts1.cn.mm.bing.net/th?id=OIP-C.KI_W63fYATztZfPLkw3PiwHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2',
//   price:200,
//   price1:200,
//   state:'售后关闭'
// }]
// after.create(data)
router.get('/after',async(req,res)=>{
  const data = await after.find()
  res.send(data)
})
module.exports = router;  